import requests
from bs4 import BeautifulSoup
import time


def fetch_list(url):
    """
    """
    ret = requests.get(url)
    print(ret)
    soup = BeautifulSoup(ret.text)
    print(ret.text)
    print(123)
    exit()
    s = soup.find('div', class_='index-table demand_table')
    print(s)
    aes = s.find_all('a')
    print(aes)
    with open('anywood_link2.txt', 'a') as w:
        for a in aes:
            if a['href'][0:15] == '/demand/detail/':
                w.write(a['href'])
                w.write("\n")
    next_page = soup.find('a', class_='next') 

    if next_page:
        return "https://anywood.com" + next_page['href']
    return None
    

def fetch_list_all():
    """
    """
    next_page = fetch_list("https://anywood.com/demand.html?root=buy&category=demand&order=pos&p=247")

    i = 0
    while next_page:
        next_page = fetch_list(next_page)
        time.sleep(1)
        print(next_page)
        i += 1
        if i % 10 == 0:
            time.sleep(600)


def fetch_detail_all():
    """
    """ 
    with open('anywood_link1.txt', 'r') as r:
        """
        """
        i =0
        with open('anywood1.txt1','a') as w:
            url = r.readline()
            while url:
                s = fetch_detail( 'https://anywood.com' + url)
                if s:
                    w.write(s)
                    w.write("\n")
                    url = r.readline()
                    i += 1
                    print(i)
                    time.sleep(1)


def fetch_detail(url):
    """
    """
    ret = requests.get(url.strip(),
    headers={
        "Cookie": "_qddac=2-4-1.1.ovt2ly.kia3aaap; pgv_pvi=4058665984; _qddaz=QD.226mzn.hpc6z3.khfzrwvz; Hm_lvt_2d65db2e4cbedcbac5a69c55e9341ee0=1605255428,1605255664,1605259311,1606984337; PHPSESSID=bbqmfvkid8s06ild02pgdnqtp4; _qddab=2-ovt2ly.kia3aaap; Hm_lpvt_2d65db2e4cbedcbac5a69c55e9341ee0=1607075796; onethink_home_API_SESSION=MDAwMDAwMDAwMIJ1sdqAoX9og56KZn-edpexp6Og; onethink_home_API_SESSION_TIME=MDAwMDAwMDAwMIGFvdyBoaFshXiGrYGrbqM; IESESSION=alive; pgv_si=s860704768; onethink_home_view_detail=think%3A%5B%22267648%22%2C%22267005%22%2C%22268804%22%2C%22269703%22%5D; _qdda=2-1.1; acw_tc=76b20f7116070755527663033e410604e23755c09acb3915bb7801d279fa81; acw_sc__v2=5fca06e05829cd1f4939b09e4b42a2fa31df0143"
    })
    
    print(ret.text)
    soup = BeautifulSoup(ret.text)

    r = soup.find('div',class_='top_left')
    
    if not r:
        return False

    spans = r.find_all('span')

    l = [] 
    for i,span in enumerate(spans):
        print(span.get_text().strip())
        print(i)
        if i in [2,7,10,12,14,16]:
            l.append(span.get_text().strip())
    s = "$$$".join(l)
    print(s)
    return s


if __name__ == "__main__":
    # fetch_detail_all()
    fetch_list_all()
    # fetch_list('https://anywood.com/demand.html?root=buy&category=demand&order=pos&p=248')